package com.qf.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.api.GoodsApi;
import com.api.JifenApi;
import com.qf.Goods;
import com.qf.Jifen;
import com.qf.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @ClassName OrderController.java
 * @createTime 2021年12月10日 14:31:00
 */
@RestController
@RequestMapping("/order")
public class OrderController {
   /* @Autowired
    RestTemplate restTemplate;*/

    /*@RequestMapping("save")
    public Map save() {
        String serVName = "cloud-goods";
        String url = "http://" + serVName + "/goods/findById/1";
        Goods object = restTemplate.getForObject(url, Goods.class);

        System.out.println("接受到的信息" + object);
        System.out.println("save order");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code", "200");
        hashMap.put("msg", "success");
        return hashMap;
    }

    @RequestMapping("test")
    public String test() {
        String serVName = "cloud-goods";
        String url = "http://" + serVName + "/goods/save";
        Goods goods = new Goods("华为", 1);
        HashMap hashMap = restTemplate.postForObject(url, goods, HashMap.class);
        System.out.println(hashMap);
        return "success";
    }

    @RequestMapping("test1")
    public String test1() {
        String serVName = "cloud-goods";
        String url = "http://" + serVName + "/goods/save";
        Goods goods = new Goods("华为", 1);
        ResponseEntity<HashMap> responseEntity = restTemplate.postForEntity(url, goods, HashMap.class);
        System.out.println(responseEntity);
        System.out.println("响应状态:" + responseEntity.getStatusCode());
        return "success";
    }*/


    @Autowired
    private JifenApi jifenApi;

    @Autowired
    private GoodsApi goodsApi;

    @Resource(name = "threadPool")
    ExecutorService threadPool;

    @RequestMapping("test1")
    public String test1() {
        Jifen jifen = new Jifen(1, 10, "12");
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        threadPool.execute(() -> {
            RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);
            Map save = jifenApi.save(jifen);
            System.out.println(save);
            Goods goods = goodsApi.findById("1");
            System.out.println(goods);
        });
        return "success";
    }

    @RequestMapping("test2")
    public String test2() {
        Jifen jifen = new Jifen(1, 10, "12");
        Map save = jifenApi.save(jifen);
        System.out.println(save);
        return "success";
    }

    @RequestMapping("test3")
    public String test3() {

        Map map = jifenApi.deletedById(1);
        System.out.println(map);
        return "success";
    }

    @Autowired
    private OrderService orderService;

    @RequestMapping("test5")
    public String test5() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        orderService.saveOrder();
        return "test5";
    }

    @RequestMapping("test6")
    @SentinelResource(value = "test6", fallback = "test6Handler")
    public String test6(String flag) {
        if (StringUtils.isBlank(flag)) {
            throw new RuntimeException("空参数");
        }
        orderService.saveOrder();
        return "test6";
    }

    public String test6Handler(String flag, Throwable throwable) {
        if (throwable instanceof DegradeException) {
            return "系统开小差，稍后再试！";
        }
        return "非sentinel异常";
    }

    @RequestMapping("test9")
    @SentinelResource(value = "test9", blockHandler = "test9Handler")
    public String test9(Throwable throwable) {

        return "test9";
    }

    public String test9Handler() {
        return "流控";
    }
}
